package scau.hotel.hotel_service.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import scau.hotel.hotel_service.entity.CheckinCusInfor;
import scau.hotel.hotel_service.mapper.CheckinCusInforMapper;
import scau.hotel.hotel_service.mapper.CheckinInforMapper;
import scau.hotel.hotel_service.service.CheckinCusInforService;
import scau.hotel.hotel_model.controller.RespBean;
import scau.hotel.hotel_model.controller.RespBeanEnum;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (CheckinCusInfor)表服务实现类
 *
 * @author makejava
 * @since 2022-03-28 20:15:16
 */
@Service("checkinCusInforService")
public class CheckinCusInforServiceImpl implements CheckinCusInforService {
    @Resource
    private CheckinCusInforMapper checkinCusInforMapper;

    @Resource
    private CheckinInforMapper checkinInforMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param checkinCusInforId 主键
     * @return 实例对象
     */
    @Override
    public CheckinCusInfor queryById(Integer checkinCusInforId) {
        return this.checkinCusInforMapper.queryById(checkinCusInforId);
    }

    /**
     * 通过IDCard查询单条数据
     *
     * @param checkinCusIdCard 查询条件
     * @return 实例对象
     */
    @Override
    public CheckinCusInfor queryByIdCard(String checkinCusIdCard) {
        return this.checkinCusInforMapper.queryByIdCard(checkinCusIdCard);
    }

    /**
     * 通过Name查询单条数据
     *
     * @param checkinCusName 查询条件
     * @return 实例对象
     */
    @Override
    public List<CheckinCusInfor> queryByName(String checkinCusName) {
        return this.checkinCusInforMapper.queryByName(checkinCusName);
    }

    /**
     * 通过CheckinInforId查询单条数据
     *
     * @param CheckinInforId 查询条件
     * @return 实例对象
     */
    @Override
    public List<CheckinCusInfor> queryByCheckinInforId(Integer CheckinInforId) {
        return this.checkinCusInforMapper.queryByCheckinInforId(CheckinInforId);
    }

    /**
     * 分页查询
     *
     * @param checkinCusInfor 筛选条件
     * @return 查询结果
     */
    @Override
    public List<CheckinCusInfor> queryAll(CheckinCusInfor checkinCusInfor) {
        return this.checkinCusInforMapper.queryAll(checkinCusInfor);
    }

    /**
     * 分页查询
     *
     * @param checkinCusInfor 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<CheckinCusInfor> queryByPage(CheckinCusInfor checkinCusInfor, PageRequest pageRequest) {
        long total = this.checkinCusInforMapper.count(checkinCusInfor);
        return new PageImpl<>(this.checkinCusInforMapper.queryAllByLimit(checkinCusInfor, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param checkinCusInfor 实例对象
     * @return 实例对象
     */
    @Override
    public RespBean insert(CheckinCusInfor checkinCusInfor) {
        this.checkinCusInforMapper.insert(checkinCusInfor);
        return RespBean.success();
    }

    /**
     * 修改数据
     *
     * @param checkinCusInfor 实例对象
     * @return 实例对象
     */
    @Override
    public RespBean update(CheckinCusInfor checkinCusInfor) {
        Integer i = this.checkinCusInforMapper.queryById(checkinCusInfor.getCheckinCusInforId()).getCheckinInforId();
        if(checkinCusInfor.getDelFlag()==0 && Objects.equals(this.checkinInforMapper.queryById(i).getStatus(), "入住中")){
            return RespBean.error(RespBeanEnum.CHECKIN_CUS_INFO_STATUS_ERROR);
        }else {
            this.checkinCusInforMapper.update(checkinCusInfor);
            return RespBean.success();
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param checkinCusInforId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer checkinCusInforId) {
        return this.checkinCusInforMapper.deleteById(checkinCusInforId) > 0;
    }
}
